

import 'package:flutter/foundation.dart';
import 'package:FiduMart/core/utils/logger.dart';
import 'package:FiduMart/core/utils/pref_utils.dart';
import 'package:FiduMart/localization/app_export.dart';
import 'package:flutter/material.dart';
import 'package:FiduMart/screen/page1/page1_controller.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebviewTwoController extends GetxController {
  WebViewController wvController = WebViewController();
  Rx<bool> loading = Rx(true);

  final pref = Get.find<PrefUtils>();
  final homeController = Get.put(Page1Controller());

  @override
  void onReady() {
    String eurl = Get.parameters['url'] ?? '';
    Logger.log('jbdsjb:${eurl}');
    if (eurl == '') {
      // 正常情况下也有值的
      Get.back();
      // Get.toNamed(AppRoutes.homebarScreen);
    }
    wvController
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      // ..clearCache()
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // loading.value = false;
            Logger.log("onProgress");
            // Update loading bar.
          },
          onPageStarted: (String url) {
            Logger.log("onPageStarted");
          },
          onPageFinished: (String url) async {
            loading.value = false;
            Logger.log("onPageFinished");
            var tokenStr = await pref.getToken();
            WebViewCookieManager cookieManager = WebViewCookieManager(); //创建CookieManager实例
            var jsonToken = '''
            {
	"token": "${tokenStr}",
}
            ''';
            if(kReleaseMode){
              cookieManager.setCookie(WebViewCookie(
                  name:"a",
                  value:jsonToken,
                  domain:".umcrypto.io"
              )); //传入要设置的Cookies列表
            }else{
              cookieManager.setCookie(WebViewCookie(
                  name:"a",
                  value:jsonToken,
                  domain:".utonmos.com"
              )); //传入要设置的Cookies列表
            }

          },
          onWebResourceError: (WebResourceError error) {
            Logger.log("onWebResourceError");
          },
          onNavigationRequest: (NavigationRequest request) {
            Logger.log("onNavigationRequest");
            // if (request.url.startsWith('https://www.hao123.com/')) {
            //   return NavigationDecision.prevent;
            // }
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(eurl));
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
